package cn.bugstack.test.domain.tag;

import cn.bugstack.domain.tag.service.TagServiceImpl;
import cn.bugstack.infrastructure.redis.IRedisService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.redisson.api.RBitSet;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

/**
 * @description 人群标签服务测试
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class ITagServiceTest {

    @Resource
    private TagServiceImpl tagService;
    @Resource
    private IRedisService redisService;

    @Test
    public void test_tag_job() {
        // 执行人群标签采集任务，添加新用户到bitset
        // tagService.execTagBatchJob("RQ_KJHKL98UU78H66554GFDV", "10001");

        // 获取BitSet，使用时动态创建 bitmap，无需提前创建
        RBitSet bitSet = redisService.getBitSet("RQ_KJHKL98UU78H66554GFDV");
        String pre = "xfg0";
        for(int i = 1; i < 10; i++) {
            bitSet.set(redisService.getIndexFromUserId(pre + i), true);
        }
    }

    @Test
    public void test_get_tag_bitmap() {
        // 从redis 获取人群标签的bitmap
        RBitSet bitSet = redisService.getBitSet("RQ_KJHKL98UU78H66554GFDV");
        log.info("bitmap:{}", bitSet);
        // 是否存在
        log.info("xiaofuge 存在，预期结果为 true，测试结果:{}", bitSet.get(redisService.getIndexFromUserId("xiaofuge")));
        log.info("xfg01 存在，预期结果为 true，测试结果:{}", bitSet.get(redisService.getIndexFromUserId("xfg01")));
        log.info("xfg02 存在，预期结果为 true，测试结果:{}", bitSet.get(redisService.getIndexFromUserId("xfg02")));
        log.info("xfg03 存在，预期结果为 true，测试结果:{}", bitSet.get(redisService.getIndexFromUserId("xfg03")));
        log.info("gudebai 不存在，预期结果为 false，测试结果:{}", bitSet.get(redisService.getIndexFromUserId("gudebai")));
    }

}
